package com.vic.exchange.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import com.vic.utils.RabbitmqUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 发送者
 */
public class Producer {
    // 交换机
    private static final String EXCHANGE_NAME = "exchange_direct";
    // 重要队列
    private static final String IMPORTANT_QUEUE_NAME = "queue_important";
    private static final String IMPORTANT_ROUTING_KEY = "important";
    // 普通队列
    private static final String NORMAL_QUEUE_NAME = "queue_normal";
    private static final String NORMAL_ROUTING_KEY = "normal";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitmqUtils.getChannel();

        // 声明一个 direct 类型的交换机
        channel.exchangeDeclare(Consts.EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        // 要发送的消息
        List<String> strList = new ArrayList<>();
        strList.add("important1");
        strList.add("important2");
        strList.add("important3");
        strList.add("normal1");
        strList.add("normal2");
        strList.add("normal3");

        if (strList != null && !strList.isEmpty()) {
            strList.forEach((item) -> {
                if (item.contains("important")) {
                    try {
                        channel.basicPublish(EXCHANGE_NAME, IMPORTANT_ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, item.getBytes(StandardCharsets.UTF_8));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        channel.basicPublish(EXCHANGE_NAME, NORMAL_ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, item.getBytes(StandardCharsets.UTF_8));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        System.out.println("发送消息完成");

    }

}
